package cn.exrick.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.exrick.common.exception.NeedLoginException;
import cn.exrick.common.exception.XmallException;
import cn.exrick.pojo.TbFriend;
import cn.exrick.pojo.TbFriendRequest;
import cn.exrick.pojo.TbMember;
import cn.exrick.pojo.common.Result;
import cn.exrick.pojo.front.Member;
import cn.exrick.service.FriendRequestService;
import cn.exrick.service.FriendService;
import cn.exrick.service.LoginService;
import cn.exrick.service.MemberService;
import cn.exrick.utils.DtoUtil;
import cn.exrick.utils.ResultUtil;
import cn.exrick.utils.mybatis.Page;
import cn.exrick.utils.mybatis.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(description = "好友管理")
public class FriendController {
	@Autowired
	FriendService friendService;
	@Autowired
	FriendRequestService friendRequestService;
	@Autowired
	LoginService loginService;
	@Autowired
	MemberService memberService;

	@RequestMapping(value = "/friend/list", method = RequestMethod.POST)
	@ApiOperation(value = "查询好友列表")
	public Result<List<TbFriend>> getList(String token) throws Exception {
		Member member = loginService.getUserByToken(token);
		if (member == null) {
			throw new NeedLoginException();
		}
		List<TbFriend> list = friendService.findByMemberId(member.getId());
		for (TbFriend friend : list) {
			TbMember m = memberService.getMemberById(friend.getFriendId());
			if (m != null) {
				friend.setFriendName(m.getUsername());
				friend.setFriendHeader(m.getFile());
				friend.setBusScope(m.getBusScope());
				friend.setBusScopeImage(m.getBusScopeImage());
				friend.setCity(m.getCity());
			}
		}
		return new ResultUtil<List<TbFriend>>().setData(list);
	}

	@RequestMapping(value = "/friend/page", method = RequestMethod.POST)
	@ApiOperation(value = "查询好友列表")
	public Result<Page<TbFriend>> getList(String token, @RequestParam(defaultValue = "0") Integer pageNo,
			@RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
		Member member = loginService.getUserByToken(token);
		if (member == null) {
			throw new NeedLoginException();
		}
		PageParam pageParam = new PageParam(pageNo, pageSize);
		Page<TbFriend> page = friendService.findPageByMemberId(member.getId(), pageParam);
		for (TbFriend friend : page.getResult()) {
			TbMember m = memberService.getMemberById(friend.getFriendId());
			if (m != null) {
				friend.setFriendName(m.getUsername());
				friend.setFriendHeader(m.getFile());
				friend.setBusScope(m.getBusScope());
				friend.setBusScopeImage(m.getBusScopeImage());
				friend.setCity(m.getCity());
			}
		}
		return new ResultUtil<Page<TbFriend>>().setData(page);
	}

	@RequestMapping(value = "/friend/request/list", method = RequestMethod.POST)
	@ApiOperation(value = "查询好友申请列表")
	public Result<List<TbFriendRequest>> getRequestList(String token) throws Exception {
		Member member = loginService.getUserByToken(token);
		if (member == null) {
			throw new NeedLoginException();
		}
		List<TbFriendRequest> list = friendRequestService.findByMemberId(member.getId());
		for (TbFriendRequest friendRequest : list) {
			TbMember m = memberService.getMemberById(friendRequest.getFriendId());
			if (m != null) {
				friendRequest.setFriendName(m.getUsername());
				friendRequest.setFriendHeader(m.getFile());
			}
		}
		return new ResultUtil<List<TbFriendRequest>>().setData(list);
	}

	@RequestMapping(value = "/friend/request/page", method = RequestMethod.POST)
	@ApiOperation(value = "查询好友申请列表")
	public Result<Page<TbFriendRequest>> getRequestList(String token, @RequestParam(defaultValue = "0") Integer pageNo,
			@RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
		Member member = loginService.getUserByToken(token);
		if (member == null) {
			throw new NeedLoginException();
		}
		PageParam pageParam = new PageParam(pageNo, pageSize);
		Page<TbFriendRequest> page = friendRequestService.findByMemberId(member.getId(), pageParam);
		for (TbFriendRequest friendRequest : page.getResult()) {
			TbMember m = memberService.getMemberById(friendRequest.getFriendId());
			if (m != null) {
				friendRequest.setFriendName(m.getUsername());
				friendRequest.setFriendHeader(m.getFile());
			}
		}
		return new ResultUtil<Page<TbFriendRequest>>().setData(page);
	}

	@RequestMapping(value = "/friend/search", method = RequestMethod.POST)
	@ApiOperation(value = "搜索好友")
	public Result<Member> search(String token, Long memberId, String phone) throws Exception {
		Member member = loginService.getUserByToken(token);
		if (member == null) {
			throw new NeedLoginException();
		}
		if (memberId == null && StringUtils.isEmpty(phone)) {
			throw new XmallException("参数不足");
		}
		TbMember member1 = null;
		if (memberId != null) {
			try {
				member1 = memberService.getMemberById(memberId);
			} catch (Exception e) {
				member1 = null;
			}
		} else {
			member1 = memberService.getMemberByPhone(phone);
		}
		if (member1 == null) {
			throw new XmallException("搜索不到该用户");
		}
		Member m = DtoUtil.TbMemer2Member(member1);
		TbFriend friend = friendService.findByFriendAndMember(member1.getId(), member.getId());
		if (friend != null) {
			m.setFriend(true);
		} else {
			m.setFriend(false);
		}
		return new ResultUtil<Member>().setData(m);
	}

	@RequestMapping(value = "/friend/add", method = RequestMethod.POST)
	@ApiOperation(value = "添加好友")
	public Result<Object> add(String token, Long memberId) throws Exception {
		Member member = loginService.getUserByToken(token);
		if (member == null) {
			throw new NeedLoginException();
		}
		if (!memberId .equals(member.getId())) {
			throw new XmallException("请勿添加自己为好友");
		}
		TbFriend friend = friendService.findByFriendAndMember(memberId, member.getId());
		if (friend != null) {
			throw new XmallException("你们已经是好友了");
		}
		List<TbFriendRequest> friendRequests = friendRequestService.findByFriendAndMember(member.getId(), memberId, 0);
		if (friendRequests.size() > 0) {
			throw new XmallException("请勿重复添加好友");
		}

		TbFriendRequest friendRequest = new TbFriendRequest();
		friendRequest.setMemberId(memberId);
		friendRequest.setFriendId(member.getId());
		friendRequest.setStatus(0);
		friendRequestService.save(friendRequest);
		return new ResultUtil<Object>().setData(true);
	}

	@RequestMapping(value = "/friend/agree", method = RequestMethod.POST)
	@ApiOperation(value = "通过申请")
	public Result<Object> agree(String token, Long requestId) throws Exception {
		Member member = loginService.getUserByToken(token);
		if (member == null) {
			throw new NeedLoginException();
		}
		TbFriendRequest friendRequest = friendRequestService.findById(requestId);
		if (friendRequest == null) {
			throw new XmallException("好友请求不存在");
		}
		if (!friendRequest.getMemberId().equals(member.getId())) {
			throw new XmallException("您不能处理其他用户的好友请求");
		}
		if (friendRequest.getStatus() != 0) {
			throw new XmallException("好友请求已处理");
		}
		friendRequest.setStatus(1);
		friendRequestService.update(friendRequest);

		TbFriend friend = friendService.findByFriendAndMember(friendRequest.getFriendId(), friendRequest.getMemberId());
		if (friend == null) {
			friend = new TbFriend();
			friend.setMemberId(friendRequest.getMemberId());
			friend.setFriendId(friendRequest.getFriendId());
			friendService.save(friend);
			friend = new TbFriend();
			friend.setFriendId(friendRequest.getMemberId());
			friend.setMemberId(friendRequest.getFriendId());
			friendService.save(friend);
		}

		return new ResultUtil<Object>().setData(true);
	}

	@RequestMapping(value = "/friend/refuse", method = RequestMethod.POST)
	@ApiOperation(value = "拒绝申请")
	public Result<Object> refuse(String token, Long requestId) throws Exception {
		Member member = loginService.getUserByToken(token);
		if (member == null) {
			throw new NeedLoginException();
		}
		TbFriendRequest friendRequest = friendRequestService.findById(requestId);
		if (friendRequest == null) {
			throw new XmallException("好友请求不存在");
		}
		if (!friendRequest.getMemberId().equals(member.getId())) {
			throw new XmallException("您不能处理其他用户的好友请求");
		}
		if (friendRequest.getStatus() != 0) {
			throw new XmallException("好友请求已处理");
		}
		TbFriend friend = friendService.findByFriendAndMember(friendRequest.getFriendId(), friendRequest.getMemberId());
		if (friend != null) {
			friendRequest.setStatus(1);
			friendRequestService.update(friendRequest);
			throw new XmallException("你们已经是好友了");
		}
		friendRequest.setStatus(2);
		friendRequestService.update(friendRequest);
		return new ResultUtil<Object>().setData(true);
	}

	@RequestMapping(value = "/friend/delete", method = RequestMethod.POST)
	@ApiOperation(value = "删除好友")
	public Result<Object> delete(String token, Long friendId) throws Exception {
		Member member = loginService.getUserByToken(token);
		if (member == null) {
			throw new NeedLoginException();
		}
		TbFriend friend = friendService.findByFriendAndMember(friendId, member.getId());
		if (friend == null) {
			throw new XmallException("你们不是好友");
		}
		friendService.delete(friend);
		friend = friendService.findByFriendAndMember(member.getId(), friendId);
		if (friend != null) {
			friendService.delete(friend);
		}
		return new ResultUtil<Object>().setData(true);
	}

}
